/*
 * Copyright (C) 2015 Jack Jiang(cngeeker.com) The BeautyEye Project.
 * All rights reserved.
 * Project URL:https://github.com/JackJiang2011/beautyeye
 * Version 3.6
 *
 * Jack Jiang PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * BETreeUI.java at 2015-2-1 20:25:36, original version by Jack Jiang.
 * You can contact author with jb2011@163.com.
 */
package com.lijinjiang.beautyeye.ch16_tree;

import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

/**
 * JTree的UI实现类。.
 * @author Li
 * @version 2.0
 */
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 一些说明 Start
// 本类的实现参考了 WindowsTreeUI
// 目前，本类中暂未对UI方面代码进行修改，对Tree的UI修改主要是基于UIManager对应Tree
// 属性的配置，目前已经足够达到预期效果，如有必要可开放本类中的代码进行深入修改。
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 一些说明 END
public class BETreeUI extends BasicTreeUI {
    private Color focusGainedColor = UIManager.getColor("Tree.focusGainedBackground") == null ?
            new Color(205, 232, 255) : UIManager.getColor("Tree.focusGainedBackground"); // 聚焦时的背景色
    private Color focusLostColor = UIManager.getColor("Tree.focusLostBackground") == null ?
            new Color(213, 213, 213) : UIManager.getColor("Tree.focusLostBackground"); // 失去焦点时的背景色


    /**
     * Creates the ui.
     * @param c the c
     * @return the component ui
     */
    public static ComponentUI createUI(JComponent c) {
        return new BETreeUI();
    }

    public void paint(Graphics g, JComponent c) {
        // 根据焦点状态绘制树节点整行的颜色
        if (tree.getSelectionModel().getSelectionCount() > 0) {
            if (tree.hasFocus()) {
                g.setColor(focusGainedColor);
            } else {
                g.setColor(focusLostColor);
            }
            for (int i : tree.getSelectionModel().getSelectionRows()) {
                Rectangle r = tree.getRowBounds(i);
                g.fillRect(0, r.y, tree.getWidth(), r.height);
            }
        }
        super.paint(g, c);
    }


    protected FocusListener createFocusListener() {
        return new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                tree.repaint();
            }

            @Override
            public void focusLost(FocusEvent e) {
                tree.repaint();
            }
        };
    }

    /**
     * 获取Path为整行
     * @param tree
     * @param path
     * @return
     */
    @Override
    public Rectangle getPathBounds(JTree tree, TreePath path) {
        if (tree != null && treeState != null) {
            return getPathBounds(path, tree.getInsets(), new Rectangle());
        }
        return null;
    }

    private Rectangle getPathBounds(
            TreePath path, Insets insets, Rectangle bounds) {
        Rectangle rect = treeState.getBounds(path, bounds);
        if (rect != null) {
            rect.width = tree.getWidth();
            rect.y += insets.top;
        }
        return rect;
    }

    protected TreeCellRenderer createDefaultCellRenderer() {
        return new WindowsTreeCellRenderer();
    }


    public class WindowsTreeCellRenderer extends DefaultTreeCellRenderer {


        @Override
        public Component getTreeCellRendererComponent(
                JTree tree, Object value, boolean selected, boolean expanded,
                boolean leaf, int row, boolean hasFocus) {
            JLabel l = (JLabel) super.getTreeCellRendererComponent(
                    tree, value, selected, expanded, leaf, row, hasFocus);
            if (hasFocus) {
                l.setBackground(selected ? focusGainedColor : tree.getBackground());
            } else {
                l.setBackground(selected ? focusLostColor : tree.getBackground());
            }
            l.setOpaque(true);
            return l;
        }


    }
}
